package com.bite.bite_forum.services.Impl;

import com.bite.bite_forum.common.AppResult;
import com.bite.bite_forum.common.ResultCode;
import com.bite.bite_forum.dao.BoardMapper;
import com.bite.bite_forum.exception.ApplicationException;
import com.bite.bite_forum.model.Article;
import com.bite.bite_forum.model.Board;
import com.bite.bite_forum.services.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.transform.Result;
import java.util.List;

@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {
    @Resource
    private BoardMapper boardMapper;

    /**
     * 获取板块记录
     * @param num  记录数
     * @return
     */

    @Override
    public List<Board> selectByNum(Integer num) {
        if(num == null){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Board> boards = boardMapper.selectByNum(num);
        return boards;
    }

    /**
     * 更新版块帖子数量
     * @param id
     * @return
     */
    @Override
    public int addArticleCountById(Long id) {
        if(id == null || id < 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() + 1);
        return boardMapper.updateByPrimaryKeySelective(updateBoard);
    }

    @Override
    public Board selectById(Long id) {
        if(id == null || id < 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return boardMapper.selectByPrimaryKey(id);
    }

    //更新帖子数量
    @Override
    public void subOneArticleCountById(Long id) {
        //校验
        if(id == null || id < 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //判断版块状态
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null || board.getDeleteState() == 1 || board.getState() == 1){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }

        //更新帖子数量
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() - 1);
        if(updateBoard.getArticleCount() < 0){
            updateBoard.setArticleCount(0);
        }
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(row != 1){
            log.info(ResultCode.ERROR_SERVICES.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES.getMessage()));
        }
    }


}
